package com.dataStructure.random;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedList;

/*




 */







public class Main {

    public static void main(String[] args) {

        int i = 213234;
        String[] split = Integer.toString(i).split("");
        for (int j = 0; j < split.length; j++) {
            String s = split[j];
            System.out.println(s);
        }

    }


    /**
     * 738 ,单调递增的数字
     *
     * split() 将字符串转为String数组
     *
     * String.join()  将字符串数组转为 字符串
     */
    public int monotoneIncreasingDigits(int n) {
        String ns = Integer.toString(n);
        String[] split = ns.split("");

        //
        for (int i = ns.length() - 2; i >= 0 ; i++) {
            if (Integer.parseInt(split[i]) >= Integer.parseInt(split[i+  1])) {
                split[i] = (Integer.parseInt(split[i]) - 1) +"";
                split[i + 1] = "9";
            }
        }
        return Integer.parseInt(String.join("",split));
    }

    /**
     * 6367. 矩阵中的和
     */
    public int matrixSum(int[][] nums) {
        int result = 0;
        for (int i = 0; i < nums.length; i++) {
            Arrays.sort(nums[i]);
        }

        for (int i = 0; i < nums.length; i++) {

        }

        return  result;
    }


    /**
     *
        6366. 老人的数目
     */
    public int countSeniors(String[] details) {
        int result = 0;
        for (String s : details) {
            String age = s.substring(0, 13);
            Integer agei = Integer.valueOf(age);
            if (agei > 60) {
                result++;
            }
        }
        return result;
    }


    /**
     * 6404. 将数组清空
     * 暴力超时
     */
    public static long countOperationsToEmptyArray(int[] nums) {
        LinkedList<Integer> list = new LinkedList<>();
        for (int e : nums) {
            list.add(e);
        }
        int ct =0;
        int min = Arrays.stream(nums).reduce(Integer.MAX_VALUE, Integer::min);

        while(list.size() > 0) {

            if (list.getFirst() != min) {
                Integer first = list.removeFirst();
                list.addLast(first);

            }
            else {
                list.removeFirst();
                if (list.size() > 0)
                min = Collections.min(list);
            }
            ct++;
        }
        return ct;
    }

    /**
     * 6405. 找到两个数组的前缀公共数组
     */
    public int[] findThePrefixCommonArray(int[] A, int[] B) {
        int[] c = new int[A.length];
        HashSet<Integer> setA = new HashSet<>();
        HashSet<Integer> setB = new HashSet<>();

        int ci = 0;
        for (int i  = 0; i < A.length; i++) {
            setA.add(A[i]);
            setB.add((B[i]));
            if (setA.contains(B[i])) {
                ci+=1;
            }
            if (setB.contains(A[i])) {
                ci+=1;
            }
            if (A[i]==B[i]) {
                ci--;
            }
            c[i] = ci;

        }

        return c;

    }

    /**
     * 6406. K 个元素的最大和
     */
    public int maximizeSum(int[] nums, int k) {
        int reduce = Arrays.stream(nums).reduce(Integer.MIN_VALUE, Integer::max);
        int max = reduce;
        for (int i = 0; i < k -1 ; i++) {
            reduce = reduce + 1;
            max += reduce;
        }

        return max;

    }

    /**
     * 338.比特位计数
     */
    public int[] countBits(int n) {
        int[] ar = new int[n + 1];
        for (int i = 0; i <= n ; i++) {
            ar[i] = Integer.bitCount(i);
        }
        return ar;
    }


    /**
     * 更好的方法是 x^y
     * 异或，相同的则为0，不同的则为1
     * Integer.bitCount()
     *
     * 二进制其他
     * 与：两个二进制位同为的1，不同的为0
     * 或：两个二进制只要一个为1，为1；不同则为0
     *
     * @param x
     * @param y
     * @return
     */
    public static int hammingDistance(int x, int y) {
        String X = Integer.toBinaryString(x);
        String Y = Integer.toBinaryString(y);
        int ct = 0;
        if (X.length() > Y.length()) {
            int dif = X.length() - Y.length();
            StringBuilder YBuilder = new StringBuilder(Y);
            for (int i = 0; i < dif; i++) {
                YBuilder.insert(0,"0");
            }
            Y = YBuilder.toString();
            for (int i = 0; i < X.length(); i++) {
                if (X.charAt(i) == Y.charAt(i)) {
                    continue;
                } else {
                    ct ++;
                }

            }
        } else {
            int dif = Y.length() - X.length();
            StringBuilder YBuilder = new StringBuilder(X);
            for (int i = 0; i < dif; i++) {
                YBuilder.insert(0,"0");
            }
            X = YBuilder.toString();
            for (int i = 0; i < Y.length(); i++) {
                if (X.charAt(i) == Y.charAt(i)) {
                    continue;
                } else {
                    ct ++;
                }

            }
        }

        return ct;
    }


}
